package com.artisan.domain.service;

import com.artisan.domain.dto.AccountDto;
import com.artisan.domain.dto.CertificateTypeDto;
import com.artisan.domain.entity.MCertificateType;
import com.artisan.domain.entity.QMCertificateType;
import com.artisan.domain.repositoryInterface.ICertificateTypeRepository;
import com.artisan.domain.serviceInterface.ICertificateTypeService;
import com.querydsl.core.QueryResults;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/** 证书类型业务类 */
@Service
public class CertificateTypeService implements ICertificateTypeService {
  @Autowired private ICertificateTypeRepository certificateTypeRepository;

  @Override
  public Page<CertificateTypeDto> findList(String name, int pageIndex, int pageSize) {
    Pageable pageable = new QPageRequest(pageIndex, pageSize); // 分页信息
    QMCertificateType certificateType = QMCertificateType.mCertificateType;
    // 初始化组装条件(类似where 1=1)
    Predicate predicate = certificateType.isNotNull().or(certificateType.isNull());
    // 执行动态条件拼装
    predicate =
        name == null || name.isEmpty()
            ? predicate
            : ExpressionUtils.and(predicate, certificateType.name.like(name));

    QueryResults<MCertificateType> result =
        this.certificateTypeRepository
            .find(predicate)
            .offset(pageable.getOffset()) // 起始页
            .limit(pageable.getPageSize()) // 每页大小
            .fetchResults();
    List<CertificateTypeDto> dtos = new ArrayList<>();
    result
        .getResults()
        .forEach(
            p -> {
              CertificateTypeDto dto = new CertificateTypeDto();

              BeanUtils.copyProperties(p, dto);
              dtos.add(dto);
            });
    Page<CertificateTypeDto> page = new PageImpl(dtos, pageable, result.getTotal());
    return page;
  }

  @Override
  public int add(CertificateTypeDto dto) {
    MCertificateType model = new MCertificateType();
    BeanUtils.copyProperties(dto, model);
    model.setAddDate(System.currentTimeMillis());
    this.certificateTypeRepository.saveAndFlush(model);
    return model.getId();
  }

  @Override
  public CertificateTypeDto find(int id) {
    MCertificateType model = this.certificateTypeRepository.findById(id).get();
    CertificateTypeDto dto = new CertificateTypeDto();
    BeanUtils.copyProperties(model, dto);
    return dto;
  }

  @Override
  public boolean update(CertificateTypeDto dto) {
    MCertificateType model = this.certificateTypeRepository.findById(dto.getId()).get();
    model.setName(dto.getName());
    model.setTrainAgency(dto.getTrainAgency());
    model.setNumberRule(dto.getNumberRule());
    model.setStartAge(dto.getStartAge());
    model.setEndAge(dto.getEndAge());
    model.setTrainCycle(dto.getTrainCycle());
    model.setUseField(dto.getUseField());
    model.setRegion(dto.getRegion());
    model.setUseRange(dto.getUseRange());
    model.setContent(dto.getContent());
    model.setStatus(dto.getStatus());
    this.certificateTypeRepository.saveAndFlush(model);
    return true;
  }

  @Override
  public boolean delete(int id) {
    this.certificateTypeRepository.deleteById(id);
    return true;
  }
}
